package com.igeek.web;

import com.igeek.domain.Customer;
import com.igeek.service.CustomerService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author jerry
 * @Company 极客营
 */
//action="${pageContext.request.contextPath }/customerServlet?method=addsubmit"
    //${pageContext.request.contextPath }/customerServlet?method=list
@WebServlet("/customerServlet")
public class CustomerAction extends HttpServlet {
    Logger logger = Logger.getLogger(CustomerAction.class);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    //action="${pageContext.request.contextPath }/customerServlet?method=addsubmit"
    //${pageContext.request.contextPath }/customerServlet?method=list
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null) return;
        if(method.equals("addsubmit")){
            addCustomer(req);
            //显示数据
            listCustomer(req,resp);
        }else if(method.equals("list")){
            listCustomer(req, resp);
        }else if(method.equals("delete")){
            //customerServlet?method=delete&cust_id=${customer.cust_id}
            deleteByCust_id(req);
            listCustomer(req, resp);
        }else if(method.equals("edit")){
            //customerServlet?method=edit&cust_id=${customer.cust_id}">修改
            edit(req, resp);
        }else if(method.equals("editsubmit")){
            //customerServlet?method=editsubmit"
            update(req);
            listCustomer(req, resp);
        }else if(method.equals("queryByCondition")){
            //customerServlet?method=queryByCondition  cust_name
            //获取所有客户数据，返回到页面
            queryByCondition(req, resp);
        }
    }

    private void queryByCondition(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        CustomerService customerService = (CustomerService) ac.getBean("customerService");
        List<Customer> customers = customerService.queryByCondition(req.getParameter("cust_name"));
        //把数据放入内存  <c:forEach items="${list}" var="customer">
        req.setAttribute("list",customers);
        req.getRequestDispatcher("jsp/customer/list.jsp").forward(req,resp);
    }

    private void update(HttpServletRequest req) {
        try {
            Customer c = new Customer();
            BeanUtils.populate(c,req.getParameterMap());
            WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            CustomerService customerService = (CustomerService) ac.getBean("customerService");
            customerService.update(c);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        CustomerService customerService = (CustomerService) ac.getBean("customerService");
        Customer c = customerService.findById(req.getParameter("cust_id"));
        req.setAttribute("customer",c);
        req.getRequestDispatcher("jsp/customer/edit.jsp").forward(req,resp);
    }

    private void deleteByCust_id(HttpServletRequest req) {
        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        CustomerService customerService = (CustomerService) ac.getBean("customerService");
        customerService.deleteById(req.getParameter("cust_id"));
    }

    private void listCustomer(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取所有客户数据，返回到页面
        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        CustomerService customerService = (CustomerService) ac.getBean("customerService");
        List<Customer> customers = customerService.findAll();
        //把数据放入内存  <c:forEach items="${list}" var="customer">
        req.setAttribute("list",customers);
        req.getRequestDispatcher("jsp/customer/list.jsp").forward(req,resp);
    }

    private void addCustomer(HttpServletRequest req) {
        try {
            //1.接收页面参数
            //1.1.创建Customer对象
            Customer c = new Customer();
            BeanUtils.populate(c,req.getParameterMap());
            logger.info(c);
            //2.调用servcie层
           // ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
          //在servlet的监听器中已经加载了applicationContext.xml，在这里不需要加载
            //spring-web 提供了一个工具类WebApplicationContextUtils，可以获取ApplicationContext
            WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            CustomerService customerService = (CustomerService) ac.getBean("customerService");
            customerService.save(c);
            //3.返回结果
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
